#include<16f877a.h>
//#device ADC=10
#include<stdio.h>

#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=16000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#define row1() init_lcd(0x80)	/* Begin at Line 1 */
#define row2() init_lcd(0xC0)   /* Begin at Line 2 */
#define LCD_Clear() init_lcd(0x01)	/* Clear display LCD */
unsigned char w[] = { 0x38,0x0c,0x01,0x06,0x80},val;
unsigned int adc_val=0;
char asg[]={"Another Crane"};
int i,c;
#define HIGH 1
#define LOW 0
#BYTE RCREG = 0x1A           //Receive char
/*#define MOT1 PIN_C0
#define MOT2 PIN_C1
#define MOT3 PIN_C2
#define MOT4 PIN_C3
void forward()
 {
output_bit(MOT1,HIGH);
output_bit(MOT2,LOW);
output_bit(MOT3,LOW);
output_bit(MOT4,HIGH);
 }*/
#INT_RDA
void SerialDataReceive()
{
	if(RCREG=='b')
   {// Store the received data byte by byte
	c++;}

}
/*======================================================LCD Initialisation============================================*/
void init_lcd(unsigned char j)
{
   output_e (0x00);
   output_d (j);
   output_e (0x02);
   delay_ms (2);
   output_e (0x00);
   delay_ms (2);
}
/*=======================================================Display Function=============================================*/
void display(char s)
{
   output_e (0x01);
   output_d (s);
   output_e (0x03);
   delay_ms (2);
   output_e (0x01);
   delay_ms (2);
}
/*=======================================================Functions For ROW============================================*/
void Lcd_init()
{
unsigned char i=0;
   for( i = 0 ; i < 6 ; i++ )
   {
      init_lcd (w[i]);
   }
}
void transmit()
{
for(i=0;i<5;i++)
   {
      putchar('a');
   delay_ms(100);
   }
}
void receive()
{
   val=getchar();
   delay_ms(100);
   if(val=='b')
   {

   }
}
void main()
{
enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
   port_b_pullups(true);
   c=0;
   //output_b(0x00);
   Lcd_init();
   row1();
   display("Crane Control");

   while(1)
   {

   transmit();
   if(c>0)
   {
   row2();
   display("Another Crane...............");
   delay_ms(5000);
   c=0;
   output_low(PIN_A5);
   delay_ms(1000);
   output_low(PIN_A2);
   delay_ms(1000);
   row2();
   display("                        ");
   delay_ms(100);
   }
   output_high(PIN_A5);
   delay_ms(1000);
   output_high(PIN_A2);
   delay_ms(1000);
   row2();
   display("Normal Load.................");
   delay_ms(100);
    if(input(PIN_B2)==0)
   {


      row2();
      display("Over Load............");

      delay_ms(100);
   output_low(PIN_A0);
      delay_ms(1000);
      output_low(PIN_A2);
   delay_ms(1000);
   row2();
   display("                        ");
   delay_ms(100);


      //delay_ms(1000);
      //delay_ms(1000);

   //output_c(0x00);

   }
   output_high(PIN_A0);
      delay_ms(1000);
      output_high(PIN_A2);
   delay_ms(1000);

  //output_low(PIN_A2);
  // delay_ms(1000);

   //delay_ms(100);
  /*if(input(PIN_B3)==1)
  {
   row2();
   display("Load is in Limit");
   delay_ms(100);


  }*/
   //delay_ms(100);
   if(input(PIN_A3)==1)
   {
      row2();
      display("Obstacle.............");
   delay_ms(100);
   putchar('a');
   delay_ms(100);


      output_low(PIN_A1);
      delay_ms(1000);
    output_low(PIN_A2);
   delay_ms(1000);
   row2();
   display("                        ");
   delay_ms(100);



    //delay_ms(1000);
     //delay_ms(1000);
   }

   output_high(PIN_A1);
      delay_ms(1000);
      output_high(PIN_A2);
   delay_ms(1000);

   /*if(input(PIN_B0)==0)
   {printf("on\n");
   }
   if(input(PIN_B0)==1)
   {printf("off\n");
   }*/

}

}
